<?php
/**
* @package byte
* @author Brilaps, LLC
* @copyright Brilaps, LLC
* @license The BSD License - http://www.opensource.org/licenses/bsd-license.php
*/

/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onInitEditor', 'botByteEditorInit' );
$_MAMBOTS->registerFunction( 'onGetEditorContents', 'botByteEditorGetContents' );
$_MAMBOTS->registerFunction( 'onEditorArea', 'botByteEditorEditorArea' );

/**
* byte editor - initialization
*/
function botByteEditorInit() {
	global $mosConfig_live_site, $adminside, $database;
	
	// Get the default stylesheet
	$query = "SELECT template FROM #__templates_menu WHERE client_id='0' AND menuid='0'";
    $database->setQuery( $query );
    $cur_template = $database->loadResult();
	// Assigned template
	if ( isset( $Itemid ) && $Itemid != "" && $Itemid != 0 ) {
		$query = "SELECT template FROM #__templates_menu WHERE client_id='0' AND menuid='$Itemid' LIMIT 1";
		$database->setQuery( $query );
		$cur_template = $database->loadResult() ? $database->loadResult() : $cur_template;
	}
	
/* Notes:
- Some YUI files are loaded up on the adminside by default.  Do not reload.
- These could be served from Yahoo! directly, but we want to enable offline editing.
*/
if ($adminside>0) {
return <<<EOD
<!-- Custom MiaCMS byte CSS Skin file -->
<link rel="stylesheet" type="text/css" href="{$mosConfig_live_site}/mambots/editors/byte/css/byte.css" />
<!-- YUI -->
<!-- Needed for Menus, Buttons and Overlays used in the Toolbar -->
<script type="text/javascript" src="{$mosConfig_live_site}/includes/js/yui/button/button-min.js"></script>
<!-- Source file for Rich Text Editor-->
<script type="text/javascript" src="{$mosConfig_live_site}/includes/js/yui/editor/editor-beta-min.js"></script>
EOD;
} else {
return <<<EOD
<!-- Include Editor CSS/JS Files -->
<!-- Skin CSS file -->
<link rel="stylesheet" type="text/css" href="{$mosConfig_live_site}/includes/js/yui/assets/skins/sam/skin.css" />
<!-- Custom Mambo Skin CSS file -->
<link rel="stylesheet" type="text/css" href="{$mosConfig_live_site}/mambots/editors/byte/css/byte.css" />
<!-- YUI -->
<!-- Utility Dependencies -->
<script type="text/javascript" src="{$mosConfig_live_site}/includes/js/yui/yahoo-dom-event/yahoo-dom-event.js"></script> 
<script type="text/javascript" src="{$mosConfig_live_site}/includes/js/yui/element/element-beta-min.js"></script> 
<!-- Needed for Menus, Buttons and Overlays used in the Toolbar -->
<script type="text/javascript" src="{$mosConfig_live_site}/includes/js/yui/container/container_core-min.js"></script>
<script type="text/javascript" src="{$mosConfig_live_site}/includes/js/yui/menu/menu-min.js"></script>
<script type="text/javascript" src="{$mosConfig_live_site}/includes/js/yui/button/button-min.js"></script>
<!-- Source file for Rich Text Editor-->
<script type="text/javascript" src="{$mosConfig_live_site}/includes/js/yui/editor/editor-beta-min.js"></script>
EOD;
}

}

/**
* byte editor - copy editor contents to form field
* @param string The name of the editor area
* @param string The name of the form field
*/
function botByteEditorGetContents( $editorArea, $hiddenField ) {
    ?>
    	<?php echo $hiddenField;?>.saveHTML();
    <?php
}

/**
* byte editor - display the editor
* @param string The name of the editor area
* @param string The content of the field
* @param string The name of the form field
* @param int The number of columns for the editor area
* @param int The number of rows for the editor area
*/

function botByteEditorEditorArea( $name, $content, $hiddenField, $width, $height, $col, $row ) {
	global $_MAMBOTS, $database;
	
	//Load byte info
	$query = "SELECT id FROM #__mambots WHERE element = 'byte' AND folder = 'editors'";
	$database->setQuery($query);
	$id = $database->loadResult();
	$mambot = new mosMambot($database);
	$mambot->load($id);

return <<<EOD
<div class="yui-skin-sam">
<textarea  id="$hiddenField" name="$hiddenField" cols="$col" rows="$row">$content</textarea>
</div>
<script type="text/javascript">
    var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event;
    
    var myConfig = {
        height: '300px',
        width: '550px',
        animate: true,
        dompath: true,
        handleSubmit: true,
        focusAtStart: true
    };

    var state = 'off';
    var $hiddenField = new YAHOO.widget.Editor('$hiddenField', myConfig);
    $hiddenField.on('toolbarLoaded', function() {
        //Custom button config
        var mosimageButton = {
            type: 'push', label: 'Insert MOS Image tag', value: 'mosimage', disabled: false
        };
        var mospageButton = {
            type: 'push', label: 'Insert MOS Pagebreak tag', value: 'mospage', disabled: false
        };
        var codeConfig = {
            type: 'push', label: 'Edit HTML Code', value: 'editcode'
        };
        this.toolbar.addButtonToGroup(mosimageButton, 'insertitem'); 
        this.toolbar.addButtonToGroup(mospageButton, 'insertitem');
        this.toolbar.addButtonToGroup(codeConfig, 'insertitem');
        
        this.toolbar.on('mosimageClick', function(o) {
           this.execCommand('inserthtml', '{mosimage}');
        }, $hiddenField, true);

       this.toolbar.on('mospageClick', function(o) {
           this.execCommand('inserthtml', '{mospagebreak}');
        }, $hiddenField, true);
        
        this.toolbar.on('editcodeClick', function() {
            var ta = this.get('element'),
                iframe = this.get('iframe').get('element');

            if (state == 'on') {
                state = 'off';
                this.toolbar.set('disabled', false);
                this.setEditorHTML(ta.value);
                if (!this.browser.ie) {
                    this._setDesignMode('on');
                }

                Dom.removeClass(iframe, 'editor-hidden');
                Dom.addClass(ta, 'editor-hidden');
                this.show();
                this._focusWindow();
            } else {
                state = 'on';
                this.cleanHTML();
                Dom.addClass(iframe, 'editor-hidden');
                Dom.removeClass(ta, 'editor-hidden');
                this.toolbar.set('disabled', true);
                this.toolbar.getButtonByValue('editcode').set('disabled', false);
                this.toolbar.selectButton('editcode');
                this.dompath.innerHTML = 'Editing HTML Code';
                this.hide();
            }
            return false;
        }, this, true);

        this.on('cleanHTML', function(ev) {
            this.get('element').value = ev.html;
        }, this, true);
        
        this.on('afterRender', function() {
            var wrapper = this.get('editor_wrapper');
            wrapper.appendChild(this.get('element'));
            this.setStyle('width', '100%');
            this.setStyle('height', '100%');
            this.setStyle('visibility', '');
            this.setStyle('top', '');
            this.setStyle('left', '');
            this.setStyle('position', '');

            this.addClass('editor-hidden');
        }, this, true);
    }, $hiddenField, true);
    $hiddenField.render();
</script>
EOD;

}
?>
